home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
6_6.lha
/
6_6
/
6_6sub_eq.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-08
|
1KB
|
62 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ substring = "abc"
ifdef NOTDEF // DELETE
include <str.h>
else // DELETE
include "6_6.h" // DELETE
endif // DELETE
ubstring &substring::operator=(char *cp)
// disconnect current shared string
if (str->p->refcnt > 1)
{
str->p->refcnt--;
srep *oldp = str->p;
int oldoffset = s - oldp->s;
str->p = new srep(oldp->s, oldp->size);
s = str->p->s + oldoffset;
}
const int stlen = strlen(cp);
// same size, simply copy data
if (len == stlen)
{
if (len > 0)
memcpy(s, cp, len);
}
// expand or shrink data area
else
{
// allocate the new data segment
char *sps = str->p->s;
const int s1len = s - sps;
const int s3len = strlen(sps) - len - s1len;
char *newdata =
new char[s1len + stlen + s3len + 1];
// copy in the data
if (s1len > 0)
memcpy(newdata, sps, s1len);
if (stlen > 0)
memcpy(newdata+s1len, cp, stlen);
if (s3len > 0)
memcpy(newdata + s1len + stlen,
s + len, s3len);
newdata[s1len + stlen + s3len] = '\0';
// replace pointers
delete str->p->s;
str->p->s = newdata;
str->p->size = s1len + stlen + s3len + 1;
s = newdata + s1len;
len = stlen;
}
return *this;